﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace SDT.Lib.Common
{
    public class Uploads
    {
        private const char Separator = '/';
        public static DateTime ConvertDate(string date)
        {
            var d = date.Split(new char[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
            return DateTime.Parse(string.Format("{1}{3}{0}{3}{2}", d[0], d[1], d[2], Separator));
        }

        /// <summary>
        /// Delete File
        /// </summary>
        /// <param name="FileName"></param>
        public static void DeleteFile(string FilePath)
        {
            try
            {
                if (File.Exists(FilePath))
                    File.Delete(FilePath);
            }
            catch { }
        }

        public static void UploadFile(HttpPostedFileBase FileToUpload, string endpath)
        {
            try
            {
                if (!Directory.Exists(endpath))
                    Directory.CreateDirectory(endpath);
                var filepath = string.Format("{0}\\{1}", endpath, FileToUpload.FileName);
                if (!File.Exists(filepath))
                    FileToUpload.SaveAs(filepath);
            }
            catch { }
        }

        public static void UploadFile(HttpPostedFileBase FileToUpload, string endpath, string prefix)
        {
            try
            {
                if (!Directory.Exists(endpath))
                    Directory.CreateDirectory(endpath);
                var filepath = string.Format("{0}\\{1}", endpath, prefix + "_" + FileToUpload.FileName);
                if (!File.Exists(filepath))
                    FileToUpload.SaveAs(filepath);
            }
            catch { }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 12/07/2011
        /// Description: Load all picture for product.
        /// </summary>
        public static List<string> GetFile(int id, string rootPath, string subPath, string formatPath, DateTime addDate)
        {
            try
            {
                List<string> listFile = new List<string>();

                if (id <= 0)
                    return listFile;
                subPath = subPath + string.Format(formatPath, addDate.Year, addDate.Month, addDate.Day, id);
                string path = rootPath + subPath;
                if (Directory.Exists(path))
                {
                    var files = new DirectoryInfo(path).GetFiles("*", SearchOption.TopDirectoryOnly);
                    foreach (var item in files)
                    {
                        listFile.Add(subPath.Replace("\\", "/") + item.Name); // Modified by Vietlv (27/07/2011: Remove character "/" on the first url )
                    }
                }

                return listFile;
            }
            catch
            {
                return new List<string>();
            }
        }
    }
}
